<?php

class letter_model extends \swap\rdb_model {
    
    const title_len_min = 6;
    const title_len_max = 45;
    
    const content_len_min = 6;
    const content_len_max = 4000;
    
    // rules
    public static function prop_rules($operation) {
        $rules = array(
            'new' => self::title_rule() + self::content_rule(),
            'edit' => self::title_rule() + self::content_rule(),
        );
        return $rules[$operation];
    }
    public static function title_rule() {
        $rule = array(
            'title' => array(
                'change_to' => 'string',
                'cannot_be' => array('', '请输入标题'),
                'char_size' => array(self::title_len_min, self::title_len_max, '标题介于%d到%d字之间'),
            ),
        );
        return $rule;
    }
    public static function content_rule() {
        $rule = array(
            'content' => array(
                'change_to' => 'string',
                'cannot_be' => array('', '请输入内容'),
                'char_size' => array(self::content_len_min, self::content_len_max, '内容介于%d到%d字之间'),
            ),
        );
        return $rule;
    }
    
    public static function get_letters_by_user_id($user_id) {
        $letters = self::get(array('user_id' => $user_id), array(array('id' => 'desc'), 0, letter_service::number_limit));
        return $letters;
    }
    
}